package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    @Override
    public List<Cart> listCart(Object...item) throws Exception {
        List<Cart> list = new ArrayList<>();
        StringBuffer sql = new StringBuffer("select b.*,f.*,c.* from business b,food f,cart c " +
                " where c.businessId = b.businessId and c.foodId = f.foodId and c.userId=?");
        if (item.length==2){
           sql.append(" and c.businessId=?");
        }
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql.toString());
            for (int i = 1; i <= item.length; i++) {
                pst.setObject(i,item[i-1]);
            }
            rs = pst.executeQuery();
            while (rs.next()){
                Cart cart = new Cart();
                cart.setCartId(rs.getInt("cartId"));
                cart.setFoodId(rs.getInt("foodId"));
                cart.setBusinessId(rs.getInt("businessId"));
                cart.setUserId(rs.getString("userId"));
                cart.setQuantity(rs.getInt("quantity"));
                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                cart.setBusiness(business);
                Food food = new Food();
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setBusinessId(rs.getInt("businessId"));
                food.setRemarks(rs.getString("remarks"));
                cart.setFood(food);
                list.add(cart);
            }
    }finally {
            DBUtil.close(pst);
        }
        return list;
    }

    @Override
    public int saveCart(Cart cart) throws Exception {
        int result =0;
        String sql = "insert into cart(foodId,businessId,userId,quantity) values(?,?,?,?)";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,cart.getFoodId());
            pst.setInt(2,cart.getBusinessId());
            pst.setString(3,cart.getUserId());
            pst.setInt(4,cart.getQuantity());
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);

        }
        return result;
    }

    @Override
    public int updateCart(Cart cart) throws Exception {
        int result =0;
        String sql = "update cart set quantity=? where userId=? and businessId=? and foodId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,cart.getQuantity());
            pst.setString(2,cart.getUserId());
            pst.setInt(3,cart.getBusinessId());
            pst.setInt(4,cart.getFoodId());
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return result;
    }

    @Override
        public int removeCart(Object...item) throws Exception {
        int result =0;
        StringBuffer sql =new StringBuffer("delete from cart where userId=? and businessId=?");
            if (item.length==3){
            sql.append(" and foodId = ?");
        }
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(String.valueOf(sql));
            for (int i = 1; i <= item.length; i++) {
                pst.setObject(i,item[i-1]);
            }
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return result;
    }
}
